﻿using AnlianCommon.Common.Domains.Addresses;
using AnlianCommon.Common.Domains.OrganizationalUnits;
using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.Departments
{
    public class DepartmentSearchHander : IRequestHandler<DepartmentSearchCommand, ResultPage<DepartmentDto>>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<Department> _departmentRepository;
        private readonly IBaseRepository<Province> _provinceRepository;
        private readonly IBaseRepository<Directly> _directlyRepository;
        private readonly IBaseRepository<County> _countyRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="departmentRepository"></param>
        /// <param name="provinceRepository"></param>
        /// <param name="directlyRepository"></param>
        /// <param name="countyRepository"></param>
        public DepartmentSearchHander(IMapper mapper, IBaseRepository<Department> departmentRepository, IBaseRepository<Province> provinceRepository, IBaseRepository<Directly> directlyRepository, IBaseRepository<County> countyRepository)
        {
            _mapper = mapper;
            _departmentRepository = departmentRepository;
            _provinceRepository = provinceRepository;
            _directlyRepository = directlyRepository;
            _countyRepository = countyRepository;
        }
        /// <summary>
        /// 部门显示查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<DepartmentDto>> Handle(DepartmentSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<DepartmentDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var departments = await (from a in  await _departmentRepository.GetAll()
                                         join b in await _countyRepository.GetAll() on a.CountyId equals b.CountyId
                                         join c in await _directlyRepository.GetAll() on b.DirectlyId equals c.DirectlyId
                                         join d in await _provinceRepository.GetAll() on c.ProvinceId equals d.ProvinceId
                                         select new DepartmentDto()
                                         {
                                             DepartmentId =a.DepartmentId,
                                             DepartmentSuperior = a.DepartmentSuperior,
                                             DepartmentName = a.DepartmentName,
                                             DepartmentDeactivatebool = a.DepartmentDeactivatebool,
                                             DepartmentTypeId = a.DepartmentTypeId,
                                             DepartmentOrdinal = a.DepartmentOrdinal,
                                             DirectSuperior = a.DirectSuperior,
                                             DepartmentSupervisor = a.DepartmentSupervisor,
                                             InChargeHead = a.InChargeHead,
                                             Treasurer = a.Treasurer,
                                             ViewablePersonnel = a.ViewablePersonnel,
                                             FunctionalSystemId = a.FunctionalSystemId,
                                             ProvinceId = d.ProvinceId,
                                             ProvinceName = d.ProvinceName,
                                             DirectlyId = c.DirectlyId,
                                             DirectlyName = c.DirectlyName,
                                             CountyId = b.CountyId,
                                             CountyName = b.CountyName,
                                             
                                         }).ToListAsync();
                if (!string.IsNullOrEmpty(request.DepartmentName))
                {
                    departments = departments.Where(p => p.DepartmentName.Contains(request.DepartmentName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.DepartmentDeactivatebool))
                {
                    departments = departments.Where(p => p.DepartmentDeactivatebool == request.DepartmentDeactivatebool).ToList();
                }
                if (!string.IsNullOrEmpty(request.DepartmentTypeId))
                {
                    departments = departments.Where(p => p.DepartmentTypeId == request.DepartmentTypeId).ToList();
                }
                result.TotalCount = departments.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                departments = departments.OrderByDescending(c => c.DepartmentId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = departments;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
